package com.sywb.chuangyebao.view;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.push.notification.BasicCustomPushNotification;
import com.alibaba.sdk.android.push.notification.CustomNotificationBuilder;
import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.utils.CacheUtils;
import org.bining.footstone.utils.SharedUtils;
import org.bining.footstone.utils.ToastUtils;
import org.bining.footstone.widget.ToggleButton;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置
 */
public class UserSettingActivity extends ActionbarActivity implements ToggleButton.OnToggleChanged {

    @BindView(R.id.tb_new_msg)
    ToggleButton tbNewMsg;
    @BindView(R.id.ll_new_msg)
    LinearLayout llNewMsg;
    @BindView(R.id.tb_sound)
    ToggleButton tbSound;
    @BindView(R.id.ll_sound)
    LinearLayout llSound;
    @BindView(R.id.tb_shake)
    ToggleButton tbShake;
    @BindView(R.id.ll_shake)
    LinearLayout llShake;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.rl_clear_cash)
    RelativeLayout rlClearCash;
    @BindView(R.id.tv_account_safe)
    TextView tvAccountSafe;
    @BindView(R.id.btn_logout)
    Button btnLogout;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.activity_user_setting;
    }

    @Override
    public void initView(Bundle bundle) {
        super.initView(bundle);
        setTitle(R.string.setting);

        boolean isMessageNotice = SharedUtils.getBoolean(Constants.ISMESSAGENOTICE, false);
        boolean isOpenSound = SharedUtils.getBoolean(Constants.ISOPENSUOUND, false);
        boolean isOpenVibration = SharedUtils.getBoolean(Constants.ISOPENVIBRATION, false);

        if (isMessageNotice) {
            tbNewMsg.setToggleOn();
        } else {
            tbNewMsg.setToggleOff();
        }

        if (isOpenSound) {
            tbSound.setToggleOn();
        } else {
            tbSound.setToggleOff();
        }

        if (isOpenVibration) {
            tbShake.setToggleOn();
        } else {
            tbShake.setToggleOff();
        }

        tbNewMsg.setOnToggleChanged(this);
        tbSound.setOnToggleChanged(this);
        tbShake.setOnToggleChanged(this);

        showCacheSize();

        if (!SharedUtils.getBoolean(Constants.USERISLOGIN, false)) {
            btnLogout.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public boolean useWindowBackground() {
        return false;
    }

    private void showCacheSize() {
        try {
            tvCache.setText(CacheUtils.getTotalCacheSize());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onToggle(View view, boolean b) {
        switch (view.getId()) {
            case R.id.tb_new_msg://新消息
                SharedUtils.put(Constants.ISMESSAGENOTICE, b);
                break;
            case R.id.tb_sound://声音
                SharedUtils.put(Constants.ISOPENSUOUND, b);
                break;
            case R.id.tb_shake://震动
                SharedUtils.put(Constants.ISOPENVIBRATION, b);
                break;
        }
        boolean isMessageNotice = SharedUtils.getBoolean(Constants.ISMESSAGENOTICE, false);
        boolean isOpenSound = SharedUtils.getBoolean(Constants.ISOPENSUOUND, false);
        boolean isOpenVibration = SharedUtils.getBoolean(Constants.ISOPENVIBRATION, false);

        BasicCustomPushNotification notification = new BasicCustomPushNotification();
        if (isMessageNotice && (isOpenSound || isOpenVibration)) {
            if (isOpenSound && isOpenVibration) {
                notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_VIBRATE_AND_SOUND);
            } else if (isOpenSound) {
                notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SOUND);
            } else {
                notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_VIBRATE);
            }
        } else {
            notification.setRemindType(BasicCustomPushNotification.REMIND_TYPE_SILENT);
        }
        boolean res = CustomNotificationBuilder.getInstance().setCustomNotification(1, notification);
    }

    @OnClick({R.id.ll_new_msg, R.id.ll_shake, R.id.ll_sound, R.id.rl_clear_cash, R.id.tv_account_safe, R.id.tv_about_us, R.id.btn_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_new_msg://新消息
                break;
            case R.id.ll_sound://声音
                break;
            case R.id.ll_shake://震动
                break;
            case R.id.rl_clear_cash://清楚缓存
                CacheUtils.cleanTotalCache();
                ToastUtils.show(mActivity, "缓存已清理");
                showCacheSize();
                break;
            case R.id.tv_account_safe://账户安全
                if (Utils.checkIsLogin()) {
                    advance(UserSafeActivity.class);
                }
                break;
            case R.id.tv_about_us:
                advance(UserAboutActivity.class);
                break;
            case R.id.btn_logout://退出登录
                HttpUtils.logout(new HttpCallback<Object>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        showProgress();
                    }

                    @Override
                    public void onSuccess(Object o) {
                        //清除本地登录数据
                        SharedUtils.put(Constants.USERISLOGIN, false);
                        SharedUtils.put(Constants.USERMOBILEVERIFY, false);
                        SharedUtils.put(Constants.USEROPENID, "");
                        SharedUtils.put(Constants.USERREFRESHTOKEN, "");
                        SharedUtils.put(Constants.USERACCESSTOKEN, "");
                        SharedUtils.put(Constants.USERAUTHTOKEN, "");
                        SharedUtils.put(Constants.USERAUTHTOKENSIG, "");
                        SharedUtils.put(Constants.HTTP_SSO_BIND, "");

                        RxBus.get().post(Constants.USERISLOGIN, "false");
                        exit();
                    }

                    @Override
                    public void onError(String strMsg) {
                        super.onError(strMsg);
                        ToastUtils.show(mActivity, strMsg);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        hideProgress();
                    }
                });
                break;
        }
    }

}
